<?php

class hotSale extends storeMan {

   private $tab = array( 'infoMan' => '信息管理');
   private $name = "热卖商品";
   private $salerType = array('上衣', '卫衣/绒衫', '毛衣/针织衫', '羽绒服/棉服', '短袖T恤', '裙子', '吊带衫/背心',
                               '裤子',  '牛仔裤', '冬棉裤');
   private $source = array( '阿里巴巴', '淘宝', '厂家' );

   public function index( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        $method = "{$a}_{$type}";
        $this->$method( $c, $a, $type );
    }

    public function index_infoMan( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        $this->table = strtolower( $c );

        $where = array( G( 'k' ) => G( 'v' ) );
        if ( $key = G( 'key' ) ) {
            $tmp = '';
            foreach ( $where as $k => $v ) {
                $tmp .= "{$k}='" . urldecode( $v ) . "' and ";
            }
            $where = $tmp ? ( $tmp . "title like '%{$key}%'" ) : '';
        }
        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];

        //echo $this->dumpSql();
        include $this->display( $c );
    }

    public function index_infoShow( $c, $a ) {

        $type = G( 'type', 'infoShow' );
        include $this->display( $c );
    }

    public function lookHotSalePic( $c, $a ) {
        $type = G( 'type', 'infoShow' );
        $this->table = strtolower( $c );

        $t=G('t');
        $where = "clothType = ".$t;

        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];


        print_r($result);


        //echo $this->dumpSql();
        include $this->display( $c );
    }

    public function addClothEvaluate( $c, $a ) {
        $type = G( 'type', 'infoShow' );
        $this->table = strtolower( $c );

        $t=G('t');
        $where = "clothType = ".$t;

        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];





        //echo $this->dumpSql();
        include $this->display( $c );
    }
    public function lookClothEvaluate( $c, $a ) {
        $type = G( 'type', 'infoShow' );
        $this->table = strtolower( $c );

        $t=G('t');
        $where = "clothType = ".$t;

        $result = $this->Paged()->findAll( $where );
        $pager = $result[ 'page' ];
        $result = $result[ 'result' ];





        //echo $this->dumpSql();
        include $this->display( $c );
    }


    public function edit( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        if ( !$id = intval( G( 'id' ) ) ) {
            exit( '非法编辑商品' );
        }
        $this->table = strtolower( $c ); // 数据库表名
        $result = $this->find( array( 'id' => $id ) ); // 查找数据
        include $this->display( $c ); // 加载模板
    }

    public function add( $c, $a ) {
        $type = G( 'type', 'infoMan' );
        include $this->display( $c );
    }

    public function save( $c ) {
        # 获取表单数据
        foreach ( $_POST as $key => $val ) {
            $rows[ $key ] = htmlspecialchars( trim( $val ) );
        }


        $rows[ 'dateline' ] = strtotime( $rows[ 'dateline' ] );
        unset( $rows[ 'attach' ] );

        $attachId = explode(',',$_POST['attach']['id']);

        # 注意 表单字段必须跟数据库表字段名一样
        $this->table = strtolower( $c ); // 数据库表名
        if ( $id = intval( G( "id" ) ) ) {
            $conditions = array( 'id' => $id );
            $msg = $this->update( $conditions, $rows ) ? 'success' : 'failed';
            $this->incrField( $conditions, 'attachments', $this->getAttachTotal() );
        } else {
            $rows[ 'attachments' ] = $this->getAttachTotal();
            for($k =0;$k < $rows[ 'attachments' ];$k++)
            {

                $sql = "select filepath from xbl_attachment where id = " .  $attachId[$k];
                $res = $this->findSql($sql);
                print_r($res);
                $rows["picAddr$k"] =$res[0]['filepath'];
            }

            $id = $this->create( $rows );
            $msg = $id ? 'success' : 'failed';
        }

        $this->updateAttachment( $id, $c );

        exit( $msg );
    }

    # 删除商品
    public function del( $c ) {
        if ( !$id = intval( G( "id" ) ) ) {
            exit( '非法删除商品' );
        }

        #删除商品
        $this->table = strtolower( $c );
        $msg = $this->delete( array( "id" => $id ) ) ? 'success' : 'failed';

        # 删除附件
        $this->table = 'attachment';
        $this->delete( array( "aid" => $id, 'model' => strtolower( $c ) ) );
        exit( $msg );
    }

    public function get_date() {
        echo date( G( 'format', $this->setting[ 'timeformat' ] ) );
    }

    # 获取上传附件总数
    private function getAttachTotal() {
        $attach = explode( ",", $_POST[ 'attach' ][ 'id' ] );
        return $attach ? ( count( $attach ) - 1 ) : 0;
    }

    # 读取附件
    private function getAttachment( $id, $c ) {
        $this->table = "attachment";
        return $this->findAll( array( 'aid' => $id, 'model' => strtolower( $c ) ) );
    }

    # 更新附件
    private function updateAttachment( $id, $c ) {
        $attach = $_POST[ 'attach' ][ 'id' ];
        if ( !$attach ) {
            return false;
        }
        $attach = rtrim( $attach, "," );
        $this->table = "attachment";
        $this->update( "id in ({$attach})", array( 'aid' => $id, 'model' => strtolower( $c ) ) );
        return true;
    }
}

?>